using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Security;
using System.Security.Permissions;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace AntEater.ComponentModel.BuildElements.Design
{

    /// <summary>
    /// Provides a <c>UITypeEditor</c> for visually selecting Task.
    /// </summary>
    public class TaskTypeEditor : UITypeEditor
    {

        private IWindowsFormsEditorService _editorService;

        /// <summary>
        /// Gets or sets the editor service.
        /// </summary>
        /// <value>The editor service.</value>
        public IWindowsFormsEditorService EditorService {
            get { return _editorService; }
            set { _editorService = value; }
        }


        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {

            if (context == null) { throw new ArgumentNullException("context"); }
            if (provider == null) { throw new ArgumentNullException("provider"); }

            string taskName = value as string;

            //Can only edit strings 
            this.EditorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

            if (this.EditorService != null) {

                IBuildElement buildElement = context.Instance as IBuildElement;

                if (buildElement != null) {

                    ListBox itemsListView = new ListBox();

                    itemsListView.BorderStyle = BorderStyle.None;

                    foreach (UsingTaskBuildElement usingTask in buildElement.Context.UsingTasks) {
                        itemsListView.Items.Add(usingTask.TaskName);
                    }
                    if (taskName != null) {
                        if (itemsListView.Items.Contains(taskName)) {
                            itemsListView.SelectedIndex = itemsListView.Items.IndexOf(taskName);
                        }
                        else {
                            itemsListView.Items.Add(taskName);
                            itemsListView.SelectedIndex = itemsListView.Items.Count - 1;
                        }
                    }
                    else {
                        if (itemsListView.Items.Count > 0) {
                            itemsListView.SelectedIndex = 0;
                        }
                    }

                    itemsListView.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);

                    this.EditorService.DropDownControl(itemsListView);

                    if (itemsListView.SelectedIndex != -1) {
                        taskName = itemsListView.SelectedItem.ToString();
                    }
                }
            }

            return taskName;
        }

        /// <summary>
        /// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <returns>
        /// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"></see> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"></see> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"></see>.
        /// </returns>
        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) {
            return UITypeEditorEditStyle.DropDown;
        }

        /// <summary>
        /// Selecteds the index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void SelectedIndexChanged(object sender, EventArgs e) {
            this.EditorService.CloseDropDown();
        }
    }
}
